package com.wn.controller;

import com.wn.dto.QueryTrainerDTO;
import com.wn.pojo.Trainer;
import com.wn.service.ITrainerService;
import com.wn.utils.Contents;
import com.wn.utils.HttpStatus;
import com.wn.utils.PageData;
import com.wn.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 培训师 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2023-06-14
 */
@Api("讲师模块的管理")
@RestController
@RequestMapping("/trainer")
public class TrainerController extends BaseController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ITrainerService trainerService;

    //查询
    @PostMapping("/list/search/{currentPage}/{pageSize}")
    public Result listSearch(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("pageSize") Integer pageSize,
            @RequestBody QueryTrainerDTO queryTrainerDTO){
        PageData pageData = trainerService.listSearch(currentPage,pageSize,queryTrainerDTO);
        return toDataResult(pageData);
    }

    //添加
    @PostMapping
    public Result addTrainer(@RequestBody Trainer trainer){

        boolean save = trainerService.save(trainer);

        String avatar = trainer.getAvatar();

        //http://192.168.31.129:9000/car/3d59fdb3-1561-46d1-b26f-ca4feb01b251.jpg
        String dbImage = avatar.substring(avatar.lastIndexOf("/") + 1);
        redisTemplate.opsForSet().add(Contents.DB_IMAGE,dbImage);

        //调用父类的 返回结果
        return toResult(save);
    }

    //修改
    @PutMapping
    public Result updateTrainer(@RequestBody Trainer trainer){

        boolean update = trainerService.updateById(trainer);
        //调用父类的 返回结果
        return toResult(update);
    }

    //删除
    @DeleteMapping ("/{id}")
    public Result deleteTrainer(@PathVariable("id") String id){
        boolean delete = trainerService.removeById(id);
        //调用父类的 返回结果
        return toResult(delete);
    }

    /**
     *
     * mysql  存储多少数据
     *
     *
     * @param ids
     * @return
     */

    @DeleteMapping ("/all")
    public Result deleteTrainerBatch(@RequestBody List<String> ids){

        //批量删除
        boolean delete = trainerService.removeBatchByIds(ids);
//        boolean delete = trainerService.removeByIds(ids);

        //调用父类的 返回结果
        return toResult(delete);
    }

    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public Result findTrainerById(@PathVariable("id") String id) {

        //查询 service 拿到 讲师
        Trainer trainer = trainerService.getById(id);

        //返回结果调用父类的方法 直接 封装  trainer 返回给前端

//        return new Result(HttpStatus.SUCCESS, "查询数据成功", trainer);
        return toDataResult(trainer);
    }

    @GetMapping("/list/all")
    public Result findTrainerAll(){

        List<Trainer> list = trainerService.list();
        return toDataResult(list);

    }

}
